<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="120px">
      <el-form-item label="任务编号" prop="assessCode">
        <el-input v-model="queryParams.assessCode" clearable></el-input>
      </el-form-item>
      <el-form-item label="任务名称" prop="assessName">
        <el-input v-model="queryParams.assessName" clearable></el-input>
      </el-form-item>
      <el-form-item label=" ">
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button plain @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="newAssess">新建</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" ref="tableRef" :data="dataList" stripe>
      <el-table-column label="序号" type="index" align="center" width="55" />
      <el-table-column label="任务编号" prop="assessCode" align="center" />
      <el-table-column label="任务名称" prop="assessName" align="center" />
      <el-table-column label="评估方法" prop="assessType" align="center" width="80">
        <template #default="{row}">
          {{ assessType.find(item => item.value == row.assessType)?.name }}
        </template>
      </el-table-column>
      <el-table-column label="评估内容" prop="assessObj" align="center">
        <template #default="{row}">
          {{ row.assessObj.split(',').map(obj => assessObj.find(item => item.value == obj)?.name).join('、') }}
        </template>
      </el-table-column>
      <el-table-column label="危险性数据" prop="seismicInputMode" align="center">
        <template #default="{ row }">
          {{ row.seismicInputMode.split(',').map(mode => seismicInputMode.find(item => item.value == mode)?.name).join('、') }}
        </template>
      </el-table-column>
      <el-table-column label="房屋数据" prop="houseDataFlag" align="center" width="120px">
        <template #default="{ row }">
          <span>{{ houseBasicData.find(item => item.value == row.houseDataFlag)?.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="createUserName" align="center" width="200" />
      <el-table-column label="操作" align="center" width="200">
        <template #default="{ row }">
          <el-button type="text" @click="assess(row)" v-if="row.currentTask == '1'">评估</el-button>
          <el-button type="text" @click="updateItem(row.id)">修改</el-button>
          <el-button type="text" @click="deleteItem(row.id, row.assessName)">删除</el-button>
          <el-button type="text" @click="setCurTask(row.id)" v-if="row.currentTask=='0'">设为当前任务</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination style="margin-top: 15px" v-show="queryParams.total > 0" :total="queryParams.total"
      v-model:page="queryParams.page" v-model:limit="queryParams.rows" @pagination="getList" ref="pagination" />

    <NewAssess v-model:open="assessOpen" />
  </div>
</template>
<script setup name="TaskManage">
import { getPage, settingTasks, deleteById } from '@/api/building/taskManage'
import { inputMethods, houseBasicData } from '@/views/building/index'
import { assessType, assessObj, seismicInputMode } from '@/views/building/index'
import NewAssess from '@/views/building/taskManage/components/newAssess.vue'
import { useRouter } from 'vue-router'

const props = defineProps({
  open: Boolean,
  url: String,
  type: String
})
const emits = defineEmits(['update:open'])
const { proxy } = getCurrentInstance()
const router = useRouter()

const queryParams = ref({
  page: 1,
  rows: 10,
  total: 0
})
const loading = ref(false)
const dataList = ref([])

const getList = () => {
  loading.value = true
  getPage(queryParams.value).then(res => {
    loading.value = false
    dataList.value = res.data?.rows || []
    queryParams.value.total = res.data?.total || 0
  }).catch(() => {
    loading.value = false
  })
}
// 查询
const handleQuery = () => {
  getList()
}

// 重置
const resetQuery = () => {
  proxy.resetForm('queryRef')
  queryParams.value.page = 1
  handleQuery()
}

// 新建
const assessOpen = ref(false)
const newAssess = () => {
  assessOpen.value = true
  proxy.$Bus.emit('clearForm')
}

proxy.$Bus.on('update-list', () => {
  queryParams.value.page = 1
  getList()
})

// 评估
const assess = (row) => {
  const {id, assessType} = row
  let assessTypeUrl = assessType == '0' ? 'building-confirm/seismic' : 'building-probability/resistEarthquakesPE'
  router.push({ path: `/building/${assessTypeUrl}` })
}
// 修改
const updateItem = id => {
  assessOpen.value = true
  proxy.$Bus.emit('getDetail', { id })
}
// 删除
const deleteItem = (id, name) => {
  proxy.$modal.confirm(`确定删除【${name}】吗？`).then(() => {
    loading.value = true
    deleteById(id).then(res => {
      if (res.code == 200) {
        proxy.$modal.msgSuccess('删除成功')
        queryParams.value.page = 1
        getList()
      }
    })
  }).catch(() => { })
}
// 设为当前任务
const setCurTask = id => {
  settingTasks(id).then(res => {
    if (res.code == 200) {
      proxy.$modal.msgSuccess('设为当前任务成功！')
      getList()
    }
  })
}
onMounted(() => {
  getList()
})

onBeforeUnmount(() => {
  proxy.$Bus.all.clear()
})
</script>
